package com.fhz.day010;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "feng";
    Uri uri;
    ContentResolver contentResolver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //访问9的数据库
        contentResolver = getContentResolver();
        uri = Uri.parse("content://com.fhz");

    }

    public void query(View view) {
        Cursor cursor = contentResolver.query(uri, new String[]{"name"}, null, null, null);
        while (cursor.moveToNext()){
            int name1 = cursor.getColumnIndex("name");
            String name = cursor.getString(name1);
//            Toast.makeText(MainActivity.this, name+""+age, Toast.LENGTH_SHORT).show();
            Log.i(TAG, "query: "+name);
        }
    }

    public void insert(View view) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("name","哈哈");
        contentValues.put("age",19);
        contentResolver.insert(uri,contentValues);
    }

    public void del(View view) {
        contentResolver.delete(uri,"name = ?",new String[]{"哈哈"});
    }

    public void update(View view) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("name","大大明");
        contentValues.put("age",20);
        contentResolver.update(uri,contentValues,"name = ?",new String[]{"大明"});
    }
}